/**
 * 
 */
package com.ms.game.itembox;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;

import com.ms.game.itembox.container.ItemBasic;
import com.ms.game.itembox.container.ItemCrystalCard;
import com.ms.game.itembox.container.ItemExtraSettings;
import com.ms.game.itembox.container.ItemFnboVoucher;
import com.ms.game.itembox.container.ItemGameCard;
import com.ms.game.itembox.container.ItemGoldCoinCard;
import com.ms.game.itembox.container.ItemLevelUpCard;
import com.ms.game.itembox.container.ItemTreasureBox;
import com.ms.game.itembox.db.ItemBoxModel;
import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.slot.base.GainRateData;
import com.ms.game.slot.base.GainRateSystem;
import com.xpec.c4.game.time.TimeConstants;
import com.xpec.c4.service.module.common.ItemInfoManager;

/**
 * 角色身上的道具資料
 * 
 * @author TW15010
 */
public class Item implements Serializable {
	private static final long serialVersionUID = -7167017637299992549L;

	/**
	 * 唯一序號
	 */
	private String sn;

	/**
	 * 數據表 {@link ItemBoxModel} 中對應的 key
	 */
	private String itemId;
	
	/** 預設道具ID*/
	private String basicItemId;

	/**
	 * 堆疊數量
	 */
	private int count;

	/**
	 * 精煉期結束時間
	 */
	private long refineEndTime;

	/**
	 * 精煉階數
	 */
	private int refineLevel;
	
	/** 
	 * 額外設定，新道具如果有其他設定就放在這裡用JSON存，要用再轉出來
	 */
	private ItemExtraSettings extraSetting;
	
	/** 過期時間*/
	private long expireTime;
	
	/**獲得此道具的方式*/
	private ItemChangeWay createItemWay;
	
	/**獲得此道具的遊戲類型*/
	private String createGameType;
	
	/**獲得此道具的遊戲館別*/
	private String createHallType;
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(sn + "|" + itemId + "|" + count);
		return sb.toString();
	}

	/**
	 * 轉成給 client 的格式
	 * 
	 * @return
	 */
	public SSimpleItem toClientFormat() {
		SSimpleItem si = new SSimpleItem();

		ItemBasic itemBasic = ItemInfoManager.getInstance().getItem(itemId);
		if(itemBasic == null){
			return null;
		}
		
		si.setSn(sn);
		si.setItemId(itemBasic.getDefaultItemId());
		si.setCount(count);
		si.setRefineEndTime(refineEndTime);
		si.setRefineLevel(refineLevel);
		si.setExpireTime(expireTime * TimeConstants.MILLI_SECOND_PRE_SECOND);
		
		ArrayList<Object> dynamicValues = new ArrayList<Object>();
		
		if(itemBasic.isCrystalCard()){ //水晶
			ItemCrystalCard crystalCard = (ItemCrystalCard) itemBasic;
			//水晶數量要另外算
			si.setCount(crystalCard.getCrystalCount() * count); 
		}
		
		if (itemBasic.isGoldCoinCard()) { // C幣卡
			ItemGoldCoinCard goldCoinCard = (ItemGoldCoinCard) itemBasic;
			dynamicValues.add(goldCoinCard.getGoldCoin());
		}
		
		if (itemBasic.isLevelUpCard()) { // 升級卡
			ItemLevelUpCard levelUpCard = (ItemLevelUpCard) itemBasic;
			dynamicValues.add(levelUpCard.getUpgradeLevel()); // 新增等級進去[0]
		}

		if (itemBasic.isTreasureBox()) { // 升級寶箱
			si.setItemId(itemId);
			ItemTreasureBox itemTreasureBox = new ItemTreasureBox();
			if(itemTreasureBox.getTreasureBoxType() == ItemTreasureBox.TYPE_RECYCLE){ //循環寶箱才從裡面拿等級，不然讀bin檔
				dynamicValues.add(extraSetting.getUseLevel());
			}
		}
		
		if(itemBasic instanceof ItemFnboVoucher){
			si.setItemId(itemBasic.getItemId());
		}
		
		if (itemBasic.isGiftBox()) { // 補償寶箱
			si.setItemId(itemId);
			ArrayList<Item> giftItem = extraSetting.getGiftItems();
			for (Item item : giftItem) {
				dynamicValues.add(item.toClientFormat()); // 新增裡面有什麼給client
			}
		}
		
		if(itemBasic.isGameCard()){ // 機台卡
			ItemGameCard gameCard = (ItemGameCard) itemBasic;
			GameCardType gameCardType = gameCard.getGameCardType();
			
			//一般的機台卡，提供可使用的gainRateID與顯示形式
			if(gameCardType != null){
				
				if(gameCardType.equals(GameCardType.ADVANCED_BET_KEY)){
					dynamicValues.add(GainRateSystem.SYSTEM_DEFAULT_GAIN_RATE_ID);
				}
				else{
					String gameType = gameCard.getUseGame().name();
					String hallType = gameCard.getUseHall();
					BigDecimal gainRate = gameCard.getGainRate();
					GainRateData data = GainRateSystem.getGainRateData(gameType, hallType, gainRate);
					dynamicValues.add(data.getId());
					dynamicValues.add(data.getGainRateStringFormat());
				}
				
			}
		}

		si.setDynamicValues(dynamicValues);
		return si;
	}

	public Item() {
	}

	public Item(PlayerItemModel itemModel) {
		initFromDataModel(itemModel);
	}

	public void initFromDataModel(PlayerItemModel model) {
		if (model == null) {
			return;
		}

		sn = model.getSn();
		itemId = model.getItemId();
		basicItemId = model.getBasicItemId();
		count = model.getCount();
		refineEndTime = model.getRefineEndTime();
		refineLevel = model.getRefineLevel();
		expireTime = model.getExpireTime();
		extraSetting = model.getExtraSettings();
		
		createItemWay = model.getCreateItemWay();
		createGameType = model.getCreateGameType();
		createHallType = model.getCreateHallType();
		
	}
	
	public String getBasicItemId() {
		return basicItemId;
	}

	public void setBasicItemId(String basicItemId) {
		this.basicItemId = basicItemId;
	}

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public long getRefineEndTime() {
		return refineEndTime;
	}

	public void setRefineEndTime(long refineEndTime) {
		this.refineEndTime = refineEndTime;
	}

	public int getRefineLevel() {
		return refineLevel;
	}

	public void setRefineLevel(int refineLevel) {
		this.refineLevel = refineLevel;
	}

	public long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}

	public ItemExtraSettings getExtraSetting() {
		return extraSetting;
	}

	public void setExtraSetting(ItemExtraSettings extraSetting) {
		this.extraSetting = extraSetting;
	}
	
	public ItemChangeWay getCreateItemWay() {
		return createItemWay;
	}

	public String getCreateGameType() {
		return createGameType;
	}

	public String getCreateHallType() {
		return createHallType;
	}
	
}
